<?php
namespace app\index\controller;

use app\common\model\Exhibition as ExhibitionModel;
use app\common\validate\Enroll;
use think\Loader;
use think\Request;
use app\common\model\Enroll as EnrollModel;
use app\common\model\Company as CompanyModel;


class Exhibition extends Base{

    /**
     * 获取活动的列表
     */
    public function index(){
        $lists = ExhibitionModel::where([])->order('exhibition_time desc')->limit(0,10)->select();
        $lists = collection($lists)->toArray();
        foreach($lists as $key => &$exhibition ){
            $exhibition['exhibition_time'] = date('Y-m-d',$exhibition['exhibition_time']);
        }
        $lists = json_encode($lists);
        $this->success($lists);
    }

    /**
     * @param Request $request
     * @throws
     */
    public function detail(Request $request){
        $id = $request->param('id');
        $exhibition = ExhibitionModel::get($id);
        $time = date('Y,m,d,H,i,s',$exhibition->exhibition_time);
        $time = explode(',',$time);
        $exhibition -> targetTime = $time;
        $this->success('获取成功','',$exhibition);
    }


    /**
     * 第一页提交的时候 也要检查一次
     * @param Request $request
     */
    public function beforeEnroll(Request $request){
        $data = $request->post();
        $validate = Loader::validate('Enroll');
        if(!$validate->scene('preEnroll')->check($data)){
            $this->error($validate->getError());
        }
        $this->success("检查通过");
    }


    /**
     * 报名
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function enroll(Request $request){
        $data = $request->post();
        $validate = Loader::validate('Enroll');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['style_ids'] = implode(',',$data['style_ids']);
        //需要对数据做验证
        //尤其手机的验证 不能从应该从缓存中取出 因为用户又改了号码怎么办呢 是吧  存储在登录信息中
        //这个功能后期 加上手机验证的时候 坐上去
        //主要需要保证 手机 和地区都有做选择

        //验证invite_code

        $data['user_id'] = $this->user->id;
        $data['openid'] = $this->user->openid;
        $data['add_time'] = $data['update_time'] = time();   //在用户中心update

        //同一个活动只能报名一次
        $count = EnrollModel::where(['user_id'=>$this->user->id,'exhibition_id'=>$data['exhibition_id']])->count();
        if($count){
            $this->error("你已经报名该活动了");
        }


        //获取B端 城运商
        //根据地区去获取
        //查看是否已经有B端数据
        /**
        'province_id'=>'require|number',
        'province_name'=>'require|chs',
        'city_id'=>'require|number',
        'city_name'=>'require|chs',
        'district_id'=>'require|number',
        'district_name'=>'require|chs',
        'company_one_id'=>'',
        'company_name'=>'',
        'mobile'=>'require|regex:\d{11}',
        'exhibition_id'=>'require|number',
        'style_ids'=>'require',
         *company_id
         */

        //查找B端运营商的上级城市运营商
        if(!empty($data['company_one_id'])){
            $company = CompanyModel::where(['id'=>$data['company_one_id']])->find();
            $data['company_two_id'] = $company['belong_to'];
        }

        $model = new EnrollModel();
        $model->data($data);
        if($model->allowField(true)->save()){
            $this->success("报名成功");
        }


        $this->error('报名失败');

    }



}